package com.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fire.api.AppService;
import com.fire.api.MerchantService;
import com.fire.api.dto.AppDTO;
import com.fire.common.domain.BusinessException;
import com.fire.common.domain.CommonErrorCode;
import com.fire.common.util.StringUtil;
import com.fire.convert.AppConvert;
import com.fire.entity.App;
import com.fire.entity.Merchant;
import com.fire.mapper.AppMapper;
import com.fire.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @author fire
 * @date 2021年07月18日12:07
 */
@DubboService
public class AppServiceImpl implements AppService {
    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     *  创建应用
     * @param merchantId
     * @param appDTO
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
//        校验合法参数
        if (merchantId ==null || appDTO ==null || StringUtil.isBlank(appDTO.getAppName())){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
//        校验商户是否如通过审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant==null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
//        查看商户资质申请状态：如果通过审核，则注册应用
        if (!"2".equals(merchant.getAuditStatus())){
            throw new BusinessException(CommonErrorCode.E_200003);
        }
//        生成应用ID
        String appId = UUID.randomUUID().toString();
//        保存商户应用信息
        App app = AppConvert.INSTANCE.dto2entity(appDTO);
        app.setAppId(appId);
//        校验商户名称唯一性
        Boolean existAppName = isExistAppName(appDTO.getAppName());
        if (existAppName ==true){
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        app.setMerchantId(merchantId);
        appMapper.insert(app);
        return AppConvert.INSTANCE.entiity2Dto(app);
    }

    /**
     * 根据商户ID查询对应的应用列表
     *
     * @param merchantId 商户ID
     * @return 应用列表
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchantId(Long merchantId) throws BusinessException {
        if (merchantId==null){
            throw new BusinessException(CommonErrorCode.E_110006);
        }
        List<App> appList = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppConvert.INSTANCE.listEntity2Dto(appList);

    }

    /**
     * 根据应用ID查询应用信息
     *
     * @param appId 应用ID
     * @return 应用具体信息
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppByAppId(String appId) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        AppDTO appDTO = AppConvert.INSTANCE.entiity2Dto(app);
        return appDTO;
    }


    //    判断应用名称是否存在
    private Boolean isExistAppName(String appName){
        App app1 = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppName,appName));
       return app1!=null;
    }
}
